package com.ruoyi.project.service.impl;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.GetLoginUser;
import com.ruoyi.project.domain.RecoveryNode;
import com.ruoyi.project.mapper.ProjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.mapper.ProjectRecoveryMapper;
import com.ruoyi.project.domain.ProjectRecovery;
import com.ruoyi.project.service.IProjectRecoveryService;

/**
 * 工程复盘目录Service业务层处理
 * 
 * @author liboyu
 * @date 2024-05-29
 */
@Service
public class ProjectRecoveryServiceImpl implements IProjectRecoveryService 
{
    @Autowired
    private ProjectRecoveryMapper projectRecoveryMapper;

    /**
     * 查询工程复盘目录
     * 
     * @param recoveryId 工程复盘目录主键
     * @return 工程复盘目录
     */
    @Override
    public ProjectRecovery selectProjectRecoveryByRecoveryId(Long recoveryId)
    {
        return projectRecoveryMapper.selectProjectRecoveryByRecoveryId(recoveryId);
    }

    /**
     * 查询工程复盘目录列表
     * 
     * @param projectRecovery 工程复盘目录
     * @return 工程复盘目录
     */
    @Override
    public List<ProjectRecovery> selectProjectRecoveryList(ProjectRecovery projectRecovery)
    {
        return projectRecoveryMapper.selectProjectRecoveryList(projectRecovery);
    }

    /**
     * 新增工程复盘目录
     * 
     * @param projectRecovery 工程复盘目录
     * @return 结果
     */
    @Override
    public int insertProjectRecovery(ProjectRecovery projectRecovery)
    {
        return projectRecoveryMapper.insertProjectRecovery(projectRecovery);
    }

    /**
     * 修改工程复盘目录
     * 
     * @param projectRecovery 工程复盘目录
     * @return 结果
     */
    @Override
    public int updateProjectRecovery(ProjectRecovery projectRecovery)
    {
        return projectRecoveryMapper.updateProjectRecovery(projectRecovery);
    }

    /**
     * 批量删除工程复盘目录
     * 
     * @param recoveryIds 需要删除的工程复盘目录主键
     * @return 结果
     */
    @Override
    public int deleteProjectRecoveryByRecoveryIds(Long[] recoveryIds)
    {
        return projectRecoveryMapper.deleteProjectRecoveryByRecoveryIds(recoveryIds);
    }

    /**
     * 删除工程复盘目录信息
     * 
     * @param recoveryId 工程复盘目录主键
     * @return 结果
     */
    @Override
    public int deleteProjectRecoveryByRecoveryId(Long recoveryId)
    {
        return projectRecoveryMapper.deleteProjectRecoveryByRecoveryId(recoveryId);
    }

    @Override
    public RecoveryNode getTree(Long projectId) {
        //1，查询数据库所有数据
        List<ProjectRecovery> list =projectRecoveryMapper.queryByProjectId(projectId,GetLoginUser.id());
        //2,创建虚拟根节点
        RecoveryNode root =new RecoveryNode();
        root.setId(0L);
        root.setLabel("根节点");
        //3,给根节点添加子节点
        findChild(root,list);
        return root;
    }

    /**
     * 递归查询子节点
     */
    private void findChild(RecoveryNode root, List<ProjectRecovery> list) {
        List<RecoveryNode> children = list.stream()
                //如果子节点的pid和根节点的id相同,则找到子节点
                .filter(item -> Objects.equals(item.getRecoveryPid(), root.getId()))
                //公园类型 转 公共的 树节点类型
                .map(item -> {
                    RecoveryNode node = new RecoveryNode();
                    node.setId(item.getRecoveryId());
                    node.setLabel(item.getRecoveryTitle());
                    node.setRecoverySort(item.getRecoverySort());
                    node.setLevel(item.getLevel());
                    node.setPid(item.getRecoveryPid());
                    findChild(node, list);
                    return node;
                }).collect(Collectors.toList());
        //如果找到了子节点，则将子节点集合放入上级的children里
        if (!children.isEmpty()){
            root.setChildren(children);
        }
    }

    @Override
    public Long getIdByPid(Long recoveryId) {
        return projectRecoveryMapper.getIdByPid(recoveryId);
    }

    @Override
    public Long checkNameUnique(ProjectRecovery projectRecovery) {
        return projectRecoveryMapper.checkNameUnique(projectRecovery);
    }
}
